#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 crane <crane@crane-pc>
#
# Distributed under terms of the MIT license.

"""

"""


# assert digit_len(10) == 2
# assert digit_len(11) == 2
# assert digit_len(1) == 1


# print( split_num(12345, 2) )
# assert split_num(12345, 3) == (12, 3, 45)
# assert split_num(12345, 1) == (1234, 5, 0)
# assert split_num(12345, 4) == (1, 2, 345)
# assert split_num(12345, 5) == (0, 1, 2345)


class Solution(object):

    # @staticmethod
    # def digit_len(n):
    #     ''' 返回n的位数
    #     '''
    #     len = 0
    #     while n:
    #         len += 1
    #         n //= 10

    #     return len

    @staticmethod
    def digit_len(n):
        ''' 返回n的位数
        '''
        return len(str(n))

    @staticmethod
    def get_count(left, mid, right, digitth, key):
        count_base = 10 ** (digitth-1)
        if key > mid or key == 0:
            return left * count_base
        if key < mid:
            return (left + 1) * count_base
        if key == mid:
            return left * count_base + right + 1

    # @staticmethod
    # def split_num(num, digitth):
    #     # 个位是第1位, 十位是第2位
    #     # 1: split_num(12345, 1) == (1234, 5, 0)
    #     # 1: split_num(12345, 3) == (12, 3, 45)
    #     # 1: split_num(12345, 4) == (1, 2, 345)

    #     left = num // (10 ** digitth)
    #     remain = num - left * (10 ** digitth)

    #     mid = remain // (10 ** (digitth-1))
    #     right = remain - mid * (10 ** (digitth-1))

    #     return left, mid, right

    @staticmethod
    def split_num(num, digitth):
        # 个位是第1位, 十位是第2位
        num_str = str(num)
        num_len = len(num_str)

        midth = num_len-digitth

        left  = num_str[0:midth]
        left = int(left) if left else 0

        mid   = int(num_str[midth])

        right = (num_str[midth+1:])
        right = int(right) if right else 0

        return left, mid, right

        # left = num_str[0:midth]
        # mid = num_str[midth]
        # right = num_str[-midth:]

    """
    @param: : An integer
    @param: : An integer
    @return: An integer denote the count of digit k in 1..n
    """
    def digitCounts(self, key, num):
        # key 0-9
        # n is "to" number
        count = 0
        len_digit = self.digit_len(num)

        for digitth in range(1, len_digit+1):
            left, mid, right = self.split_num(num, digitth)
            # print("%s, %s, %s, %s" % (left, mid, right, digitth))
            cur_count = self.get_count(left, mid, right, digitth, key)
            # print('count is %s' % cur_count)
            count += cur_count

        if key == 0:
            # for first 0
            count  += 1
        return count


def main():
    print("start main")
    s = Solution()
    print(s.digitCounts(1, 12))
    print(s.digitCounts(1, 11))
    # print(s.digitCounts(2, 11))
    print(s.digitCounts(0, 11))
    print(s.digitCounts(0, 110))
    print(s.digitCounts(0, 19))

if __name__ == "__main__":
    main()
